package com.epfresh.supplier.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.constant.Constant;
import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.H5OrderDetailActivity;
import com.epfresh.supplier.activity.test_report.TestReportDetailActivity;
import com.epfresh.supplier.entity.BookGoodsMonth;
import com.epfresh.supplier.entity.TestReport;

import java.util.List;

/**
 * 检测报告adapter
 */
public class TestReportExpandListAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    public interface OnCheckChangeListener {
        public void onCheckChange(boolean isCheck);
    }
    private OnCheckChangeListener onCheckChangeListener;
    public ExpandableListView lv;

    public Context context;
    public List<TestReport.SupplyOrderIncomeDayListBean> groupList;
    private AdapterView.OnItemClickListener onItemClickListener;

    private OnBottomClickListener onBottomClickListener;
    private OnNotifyDataSetChangedListener onNotifyDataSetChangedListener;


    public TestReportExpandListAdapter(Context context, ExpandableListView lv, List<TestReport.SupplyOrderIncomeDayListBean> groupList) {
        this.context = context;
        this.groupList = groupList;
        this.lv = lv;
    }

    public OnCheckChangeListener getOnCheckChangeListener() {
        return onCheckChangeListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    @Override
    public void notifyDataSetChanged() {
        if (onNotifyDataSetChangedListener != null) {
            onNotifyDataSetChangedListener.OnNotifyDataSetChangedListener(groupList.size());
        }
        expendList();
        super.notifyDataSetChanged();
    }


    public OnNotifyDataSetChangedListener getOnNotifyDataSetChangedListener() {
        return onNotifyDataSetChangedListener;
    }

    public void setOnNotifyDataSetChangedListener(OnNotifyDataSetChangedListener onNotifyDataSetChangedListener) {
        this.onNotifyDataSetChangedListener = onNotifyDataSetChangedListener;
    }

    public OnBottomClickListener getOnBottomClickListener() {
        return onBottomClickListener;
    }

    public void setOnBottomClickListener(OnBottomClickListener onBottomClickListener) {
        this.onBottomClickListener = onBottomClickListener;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<TestReport.SupplyOrderIncomeDayListBean.DetailListBean> configs = groupList.get(groupPosition).getDetailList();
        return configs == null ? 0 : configs.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).getDetailList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
     *
     * @return
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_date, null, false);
            vh.tv_date =convertView.findViewById(R.id.tv_date);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.tv_date.setText(groupList.get(groupPosition).getQueryDate());
        convertView.setTag(vh);
        return convertView;
    }

    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild   子元素是否处于组中的最后一个
     * @param convertView   重用已有的视图(View)对象
     * @param parent        返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder vh = null;
        if (convertView == null) {
            vh = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_date_child, null, false);
            vh.tv_product_name =convertView.findViewById(R.id.tv_product_name);
            vh.tv_enter_area_date =convertView.findViewById(R.id.tv_enter_area_date);
            vh.test_item =convertView.findViewById(R.id.test_item);
            vh.test_result_small = convertView.findViewById(R.id.test_result_small);
            vh.fl_item = convertView.findViewById(R.id.fl_item);
            convertView.setTag(vh);
        } else {
            vh = (ChildViewHolder) convertView.getTag();
        }

        TestReport.SupplyOrderIncomeDayListBean logisticsOrder = groupList.get(groupPosition);
        List<TestReport.SupplyOrderIncomeDayListBean.DetailListBean> orders = logisticsOrder.getDetailList();
        vh.tv_product_name.setText(orders.get(childPosition).getProductName());//设置商品名称
        vh.tv_enter_area_date.setText(orders.get(childPosition).getEnterAreaDate());//设置进场时间
        vh.test_item.setText(orders.get(childPosition).getTestItem());//设置检测项目
        if (orders.get(childPosition).isQualified()){
        vh.test_result_small.setImageResource(R.mipmap.qualified_small);//合格
        }else {
        vh.test_result_small.setImageResource(R.mipmap.un_qualified_small);//不合格
        }

        vh.fl_item.setTag(R.id.item_key_position, groupPosition * 100 + childPosition);
        vh.fl_item.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            switch (v.getId()) {
                case R.id.ll_item:
                    Log.i("点击了","__id"+position/100+"___"+position%100);
                    Intent intent = new Intent(context, TestReportDetailActivity.class);
                    intent.putExtra("orderId", groupList.get(position/100).getDetailList().get(position%100).getId());
                    context.startActivity(intent);
                    break;
            }
        }
    }


    /**
     * 是否选中指定位置上的子元素。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    /**
     * 展开组
     */
    protected void expendList() {

        if (lv != null)
            for (int i = 0; i < groupList.size(); i++) {
                lv.expandGroup(i);
            }
    }

    /**
     * 组的ViewHolder
     */
    public class ViewHolder {
        TextView tv_date;
    }

    /**
     * 子的ViewHolder
     */
    public class ChildViewHolder {
        TextView tv_product_name;//商品名称
        TextView tv_enter_area_date;//进场日期
        TextView test_item;//检测项目
        ImageView test_result_small;//检测结果(合格or不合格)
        View fl_item;
    }

    public interface OnBottomClickListener {

        public void onBottomClickListener(int position, int type);
    }

    public interface OnNotifyDataSetChangedListener {

        public void OnNotifyDataSetChangedListener(int size);
    }
}
